package com.aurora.demo.api;

import com.aurora.demo.api.param.MetaParam;
import com.aurora.demo.entity.Meta;
import com.aurora.demo.service.*;
import com.aurora.demo.util.Result;
import com.aurora.demo.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "4.Meta相关接口")
@RestController
@RequestMapping("/meta")
public class MetaController {

    @Autowired
    private MetaService metaService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private ThumbService thumbService;

    @Autowired
    private KeogramService keogramService;

    @Autowired
    private FeatureService featureService;

    @PutMapping("/updateManualtype")
    public Result updateManualtype(@RequestBody MetaParam metaParam) {
        String name = metaParam.getName();
        String manualtype = metaParam.getManualtype();
        metaService.updateManualtype(name, manualtype);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation("上传新的图片")
    @PostMapping("/save")
    public Result save(@RequestParam("file") MultipartFile file) {
        if(file == null) {
            return ResultGenerator.genFailResult("请选择要上传的图片");
        }
        if(file.getSize() > 1024 * 1024 * 10) {
            return ResultGenerator.genFailResult("上传图片不能大于10MB");
        }
        String fileName = file.getOriginalFilename();
        metaService.save(fileName);
        imageService.save(file);
        thumbService.save(file);
        keogramService.save(file);
        featureService.save(file);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation("根据名字查找元数据")
    @GetMapping("/find")
    public Result<Meta> findByName(@RequestParam String name) {
        return ResultGenerator.genSuccessResult(metaService.findByName(name));
    }

}
